-
-
Notifications
You must be signed in to change notification settings - Fork 360
Lisp bogo sort #512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lisp bogo sort #512
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple things to fix :)
do | ||
(rotatef | ||
(nth i list) | ||
(nth (random (length list)) list)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct (if you are implementing Fisher-Yates algorithm), you should pick a second index j
in the range i <= j < length list
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually implementing the Knuth shuffle (see here) and it's really hard not to copy the code straight so this was the best I could do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you know shuffling algorithms which are faster or easier to implement in FP, please let me know. The current one is not very efficient: nth
and rotatef
both have O(n).
The knuth shuffle seems to be designed for arrays instead of lists.
Bogo sort is, on the other hand, not exactly made to be very efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Knuth shuffle is another name for Fisher-Yates. You have to either loop for i
from 0
to length list - 2
and pick j
from i
to length list - 1
(incl) OR loop for i
from length list - 1
to 0
and pick j
from 0
to i
(incl). Right now you're doing a mix of both.
And yeah, it's not very efficient. In my Haskell implementation I transformed the list into a map, made the n
swaps, and back into a list. Going through an array would possibly be better, I'm not sure.
Lists are horrible data structures in many cases, but they are simple, it's fine for learning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I got the shuffle method down, (I even found a nice shortcut for in- and decrementing by one) and I'm not going to bother using arrays. Lists are clear and easy to use and this is bogo sort after all.
"Checks if a list is sorted" | ||
(if (< (length list) 2) | ||
t | ||
(if (< (first list) (second list)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be <=
I think
Also you didn't include it in the |
I think I fixed it? |
In the spirit of bogo sort I've patched this one together rather quickly, the shuffle is slow and the sortedp could maybe be simpler. But it overal works, ship it! :)